import 'dart:convert';

import 'package:gzmall_app/model/user_membership.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:shared_preferences/shared_preferences.dart';

part 'user.g.dart';

@JsonSerializable()
class User {
  static bool isInLogin = false;

  @JsonKey(name: 'id')
  int id;

  @JsonKey(name: 'nickname')
  String nickname;

  @JsonKey(name: 'sex')
  String sex;

  @JsonKey(name: 'auth_token')
  String authToken;

  @JsonKey(name: 'phone')
  String phone;

  @JsonKey(name: 'avatar')
  String avatar;

  @JsonKey(name: 'favorite_spus_count')
  int favoriteSpusCount;

  @JsonKey(name: 'favorite_shops_count')
  int favoriteShopsCount;

  @JsonKey(name: 'favorite_brands_count')
  int favoriteBrandsCount;

  @JsonKey(name: 'spu_tracks_count')
  int spuTracksCount;

  @JsonKey(name: 'moneybag')
  String moneybag;

  @JsonKey(name: 'membership')
  UserMembership userMembership;

  @JsonKey(name: 'share_title')
  String shareTitle;

  @JsonKey(name: 'share_content')
  String shareContent;

  @JsonKey(name: 'share_avatar')
  String shareAvatar;

  @JsonKey(name: 'share_url')
  String shareUrl;

  User(
    this.id,
    this.nickname,
    this.sex,
    this.authToken,
    this.phone,
    this.avatar,
    this.favoriteSpusCount,
    this.favoriteShopsCount,
    this.favoriteBrandsCount,
    this.spuTracksCount,
    this.moneybag,
    this.userMembership,
    this.shareTitle,
    this.shareContent,
    this.shareAvatar,
    this.shareUrl,
  );

  factory User.fromJson(Map<String, dynamic> srcJson) =>
      _$UserFromJson(srcJson);

  Map<String, dynamic> toJson() {
    return _$UserToJson(this);
  }

  User.empty();

  getUserInfo() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    id = prefs.getInt('user_id') ?? 0;
    nickname = prefs.getString('user_nickname') ?? "";
    sex = prefs.getString('user_sex') ?? "";
    authToken = prefs.getString('user_auth_token') ?? "";
    phone = prefs.getString('user_phone') ?? "";
    avatar = prefs.getString('user_avatar') ?? "";
    favoriteSpusCount = prefs.getInt('favorite_spus_count');
    favoriteShopsCount = prefs.getInt('favorite_shops_count');
    favoriteBrandsCount = prefs.getInt('favorite_brands_count');
    spuTracksCount = prefs.getInt('spu_tracks_count');
    moneybag = prefs.getString('moneybag');
    String userMembershipJson = prefs.getString('membership');
    if (userMembershipJson != null && userMembershipJson.isNotEmpty) {
      userMembership = UserMembership.fromJson(json.decode(userMembershipJson));
    }
    shareTitle = prefs.getString('share_title');
    shareContent = prefs.getString('share_content');
    shareAvatar = prefs.getString('share_avatar');
    shareUrl = prefs.getString('share_url');
    print("user_data " +
        " id " +
        id.toString() +
        " nickname " +
        nickname +
        " sex " +
        sex +
        " authToken " +
        authToken +
        " phone " +
        phone +
        " avatar " +
        avatar +
        " favorite_spus_count " +
        favoriteSpusCount.toString() +
        " favorite_shops_count " +
        favoriteShopsCount.toString() +
        " favorite_brands_count " +
        favoriteBrandsCount.toString() +
        " spu_tracks_count " +
        spuTracksCount.toString() +
        " moneybag " +
        moneybag.toString() +
        " membership ${userMembership?.toJson()?.toString() ?? ""}"
            " share_title ${shareTitle}"
            " share_content ${shareContent}"
            " share_avatar ${shareAvatar}"
            " share_url ${shareUrl}");
  }

  static saveUserInfo(User user) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt('user_id', user.id ?? 0);
    await prefs.setString('user_nickname', user.nickname ?? "");
    await prefs.setString('user_sex', user.sex ?? "");
    await prefs.setString('user_phone', user.phone ?? "");
    await prefs.setString('user_avatar', user.avatar ?? "");
    await prefs.setInt('favorite_spus_count', user.favoriteSpusCount ?? 0);
    await prefs.setInt('favorite_shops_count', user.favoriteShopsCount ?? 0);
    await prefs.setInt('favorite_brands_count', user.favoriteBrandsCount ?? 0);
    await prefs.setInt('spu_tracks_count', user.spuTracksCount ?? 0);
    await prefs.setString('moneybag', user.moneybag ?? "");
    await prefs.setString(
        'membership',
        user?.userMembership?.toJson() != null
            ? json.encode(user?.userMembership?.toJson())
            : "");
    await prefs.setString('share_title', user.shareTitle ?? "");
    await prefs.setString('share_content', user.shareContent ?? "");
    await prefs.setString('share_avatar', user.shareAvatar ?? "");
    await prefs.setString('share_url', user.shareUrl ?? "");
  }

  static saveUserToken(String token) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString('user_auth_token', token ?? "");
  }

  static cleanUserInfo() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.remove('user_id');
    await prefs.remove('user_nickname');
    await prefs.remove('user_auth_token');
    await prefs.remove('user_sex');
    await prefs.remove('user_phone');
    await prefs.remove('user_avatar');
    await prefs.remove('favorite_spus_count');
    await prefs.remove('favorite_shops_count');
    await prefs.remove('favorite_brands_count');
    await prefs.remove('spu_tracks_count');
    await prefs.remove('moneybag');
    await prefs.remove('membership');
    await prefs.remove('share_title');
    await prefs.remove('share_content');
    await prefs.remove('share_avatar');
    await prefs.remove('share_url');
  }

  static cleanUserToken() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.remove('user_auth_token');
  }

  static Future<String> getUserToken() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getString('user_auth_token');
  }

  static Future<bool> isLogin() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String token = prefs.getString('user_auth_token');
    return token != null && token.isNotEmpty;
  }

  static saveIsShowPrivacy(bool isShow) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setBool('is_show_privacy', isShow ?? false);
  }

  static Future<bool> getIsShowPrivacy() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getBool('is_show_privacy') ?? false;
  }
}
